package com.yicheng.controller;

import com.yicheng.controller.utils.R;
import com.yicheng.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@CrossOrigin
@RequestMapping("/news")
public class NewsController {

    @Autowired
    private NewsService newsService;

    @GetMapping("/getNews")
    public R getNews(Integer id) {
        R r = new R();
        if(id==null){
            try {
                r.setList(newsService.getNews());
                r.setMsg("获取资讯成功");
            } catch (Exception e) {
                r.setStatus(false);
                r.setMsg(e.getMessage());
            }
        }else{
            try {
                r.setList(newsService.getById(id));
                r.setMsg("获取该资讯成功");
            }catch (Exception e){
                r.setMsg(e.getMessage());
                r.setStatus(false);
            }
        }

        return r;
    }
}
